//
//  BaseNewsView.m
//  iCarStyle
//
//  Created by liunian on 14-2-19.
//  Copyright (c) 2014年 liunian. All rights reserved.
//

#import "BaseNewsView.h"
#import "ArticleManager.h"
#import "NewsEntity.h"

@interface BaseNewsView ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, retain) UITableView *mainTable;
@property(nonatomic, assign) CGFloat     cellHeight;
@property(nonatomic, assign) ArticleManager *articleManager;
@end
@implementation BaseNewsView

- (void)dealloc
{
    RELEASE_SAFELY(_mainTable);
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame Catalog:(CatalogType)type
{
    self = [super initWithFrame:frame];
    if (self)
    {
        // Initialization code
        self.isFirstRequest = YES;
        _catalogType = type;
        self.objectReq = [[ReqObject alloc] initWithCatalogType:type pageNum:1];
        [self addSubview:self.mainTable];

    }
    return self;
}

- (void)reloadTable
{
    SEL showNoteSel = @selector(showNoteBtn:);
//    if([self.dataSource count] <= 0)
//    {//没有数据则显示去任务大厅按钮
//        if([self respondsToSelector:showNoteSel])
//            [self performSelector:showNoteSel withObject:(id)YES];
//        [self showNoDataView];
//    }
//    else
//    {
//        if([self respondsToSelector:showNoteSel])
//            [self performSelector:showNoteSel withObject:(id)NO];
//        [self hideNoDataView];
//    }
    [self.mainTable setNeedsLayout];
    [self.mainTable reloadData];
    
}

- (void)layoutSubviews{
    [super layoutSubviews];
    [self.mainTable layoutSubviews];
    [self.mainTable setNeedsLayout];
    [self.mainTable reloadData];
}

#pragma mark -- BaseTaskHallViewDelegate

- (void)loadData{
    [[ArticleManager sharedManager] reloadNewsWithReqObject:self.objectReq itemsBlock:^(NSArray *items) {
        [self.objectReq setIsLoading:NO];
        [self endRefreshState];
        [self hideLoading];
        BMLog(@"mkManagerDidFinishedWithArticles");
        self.isFirstRequest = NO;
        if (_footRefreshView.state == RefreshLoading)
            {
            [self endFooterLoadState];
            }
        else
            {
            [self endHeaderRefreshState];
            [self.dataSource removeAllObjects];

            }
        [self showFooter:YES];

        [self.dataSource addObjectsFromArray:items];
        [self requestDataFinished];
        [self reloadTable];
        
    }];

}
- (void)loadMore
{
    if (self.objectReq.isLoading) {
        return;
    }
    [self.objectReq setIsLoading:YES];
    self.objectReq.pageNum++;
    [self loadData];

}
- (void)reload{
    if (self.objectReq.isLoading) {
        return;
    }
    self.objectReq.pageNum = 1;
    [self.objectReq setIsLoading:YES];
    [self loadData];
   }
- (void)showLoading
{
    SEL callBack = @selector(showLoading);
    if(_delegate && [_delegate respondsToSelector:callBack])
    {
        [_delegate performSelector:callBack];
    }
}

- (void)hideLoading
{
    SEL callBack = @selector(hideLoading);
    if(_delegate && [_delegate respondsToSelector:callBack])
    {
        [_delegate performSelector:callBack];
    }
}

//刷新数据,toast 暂无新的任务
- (void)refreshData
{
    if(self.isFirstRequest)
    {
        [self showLoading];
    }
    [self reload];
}

- (void)showNoDataView
{
    SEL callBack = @selector(showNoDataView:);
    if([_delegate respondsToSelector:callBack])
    {
        [_delegate performSelector:callBack withObject:self.mainTable];
    }
}

- (void)hideNoDataView
{
    SEL callBack = @selector(hideNoDataView);
    if([_delegate respondsToSelector:callBack])
    {
        [_delegate performSelector:callBack];
    }
}

- (void)changeNoDataViewState
{
    if(self.isFirstRequest)
    {//如果还没请求则先移除
        [self hideNoDataView];
        return;
    }
//    if([self.dataSource count] <= 0)
//    {//没有数据则显示去任务大厅按钮
//        [self showNoDataView];
//    }
//    else
//    {//有数据则移除nodataView
//        [self hideNoDataView];
//    }
}

#pragma mark -- HTTP
- (void)endRefreshState
{
    [self.headRefreshView endRefresh];
}
- (void)requestDataFinished{}
#pragma mark -- getter
- (UITableView*)mainTable
{
    if(!_mainTable)
    {
        _mainTable = [[UITableView alloc]initWithFrame:self.bounds style:UITableViewStylePlain];
        [_mainTable setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
        [_mainTable setBackgroundColor:[UIColor clearColor]];
        _mainTable.delegate   = self;
        _mainTable.dataSource = self;
    }
    
    return _mainTable;
}

- (UIScrollView*)mainList
{
    return self.mainTable;
}
- (void)showMessage:(Article *)article animated:(BOOL)animated {
    if (_clickDelegate && [self.clickDelegate respondsToSelector:@selector(toItemViewControllerWithArticle:)]) {
        [_clickDelegate toItemViewControllerWithArticle:article];
    }
}
- (void)configureCell:(ArticleCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    Article *article = [self.dataSource objectAtIndex:indexPath.row];
    [cell updateWithArticle:article];
}

#pragma mark -- UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.cellHeight;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"list";
    ArticleCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (nil == cell) {
        cell = [[ArticleCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    [self configureCell:cell atIndexPath:indexPath];
    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = indexPath.row;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Article *article = [self.dataSource objectAtIndex:indexPath.row];
    [self showMessage:article animated:YES];

}

- (CGFloat)cellHeight{
    return kHeightArticleCell;
}
#pragma mark -- getter
- (void)setCatalogType:(CatalogType)catalogType{
    if (catalogType != _catalogType) {
        _catalogType = catalogType;
    }
}
- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _dataSource;
}
@end
